<?php

/**
 * myaccount actions.
 *
 * @package    Nika
 * @subpackage myaccount
 * @author     Alexey Shapran (alexey.shapran@gmail.com)
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class myaccountActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		$curacc = $request->getParameter('account');
		if ($curacc!=NULL)
		{
			$this->user = sfGuardUser::retrievaccountbyid($curacc);
		}
		else
		{
			$this->myid = $this->getUser()->getGuardUser()->getId();
			$this->user = sfGuardUser::retrievaccountbyid($this->myid);
		}
		
		 /*$q = Album::retrieveuseralbumquery($curacc);
		 $this->albums = $q->execute();*/
		
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->myid = $this->getUser()->getGuardUser()->getId();
		$this->form = new sfGuardUserForm(sfGuardUser::retrievaccountbyid($this->myid));
	}

	public function executeSave(sfWebRequest $request)
	{
	 if ($request->isMethod('post'))
		{
			
			$imagefile = $request->getFiles('image');
			$delim = $request->getParameter('image_delete');
					
			$this->myid = $this->getUser()->getGuardUser()->getId();
			$this->form = new sfGuardUserForm(sfGuardUser::retrievaccountbyid($this->myid));
			$this->form-> bind(array(
				'firstname' => strip_tags($request->getParameter('firstname')),
				'lastname' => strip_tags($request->getParameter('lastname')), 			
				'additional' => strip_tags($request->getParameter('additional')), 
				'image'=> $request->getParameter('image') ,
				'username' => strip_tags($request->getParameter('username')), 
				'password' => strip_tags($request->getParameter('password'))),						
			array('image'=> $imagefile));

			if ($this->form->isValid())
			{
				$this->form->save();
				$this->getUser()->setFlash('profile_updated','1');
			}




			if($delim=='on')
			{
				$img = $this->form->getObject()->getImage();
				if(file_exists( sfConfig::get('sf_upload_dir')."/avatars/$img" ) )
				{
					unlink( sfConfig::get('sf_upload_dir')."/avatars/$img") ;

					// УДАЛЯЕМ ШАБЛОНЫ В ПОДКАТАЛОГЕ

					$dir = sfConfig::get('sf_upload_dir')."/avatars/thumb/";
					
					if ($dh = opendir($dir))
					{
						// читаем и выводим все элементы
						// от первого до последнего
						while (($file = readdir($dh)) != false)
						{

							if(stripos($file, "$img"))
							unlink($dir.$file);
						}
						// закрываем каталог
						closedir($dh);
					}




					$this->form->getObject()->setImage(NULL);
					$this->form->getObject()->save();
						
				}
			}

		}



		$this->redirect('myaccount/index');
	}
	public function executeShowmeallusers(sfWebRequest $request) {
		$maxUsersPerPage = 30;
		$this->pager = new sfDoctrinePager('sfGuardUser',$maxUsersPerPage);
		$this->pager->setQuery(sfGuardUser::getAllUsers());
		$this->pager->setPage($request->getParameter('page','1'));
		$this->pager->init();

		if (count($this->pager->getResults()) < 1) $this->redirect('default/index');
//		if ( (count($this->pager->getResults()) < 1) && ( $request->getParameter('page') > 1) ) $this->redirect('myaccount/shawmeallusers?page='.($request->getParameter('page')-1));
	}
}